
 /*
// All the arguments are optional 
// Year - If this parameter is not set, then current year will be used 
// Month - If this parameter is not set, then current month and the given Year / Current year will be shown 
// Day - this parameter can be used for selecting a particular day 
// ContainerId - If this parameter is set, then 
// * if an element by that id exist, then the calendar will be inserted in that element 
// * else an element by that id will be created and the calendar will be inserted in that element 
// If this parameter is not set then an id will be randomly generated 
// If the id of the Container Element will be returned by the function on success 
// ClassName - This parameter can be used to apply different themes for different calendars used in the same page 
*/


function Calendar(Year, Month, Day, ContainerId, ClassName, EmbeddedData, linktype) 
{ 
    //If no parameter is passed use the current date. 
    this.oDate = new Date(); 
    this.Year = (Year == null) ? this.oDate.getFullYear() : Year; 
    this.Month = (Month == null) ? this.oDate.getMonth() : Month - 1; 
    this.Day = (Day == null) ? 0 : Day; 
    this.oDate = new Date(this.Year, this.Month, 1); 
    this.NextMonth = new Date(this.Year, this.Month + 1, 1); 
    this.WeekStart = this.oDate.getDay(); 
    this.PanelSize = 20; 
    this.TableSize = 140; 
    this.Data  = EmbeddedData ? EmbeddedData.month : null;
    this.style = EmbeddedData ? EmbeddedData.style : null;
    this.mask  = EmbeddedData ? EmbeddedData.weekmask : null;
    this.linktype = linktype; 
    // Get the number of months in current month 
    this.MonthDays = Math.round((this.NextMonth.getTime() - this.oDate.getTime()) / 86400000) + 1; 
    
	if (!window.datepicker) window.datepicker=new Array();
	this.id = window.datepicker.length;
	//window.datepicker.push (this);
	window.datepicker[this.id] = this;
	//alert (this.id)
	this.setdate = function (num)
	{
    	this.Day = num; 
		this.create ();
		this.Container.innerHTML = this.HTML;  
		this.onupdate();
	}
	
	this.setmonth = function (num)
	{ 
    	this.oDate = new Date(this.Year, num, 1);  // alert (this.oDate);
    	this.mode = 0;  
		this.update();
	}
	
	this.setyear = function (num)
	{
    	this.oDate = new Date(num, this.Month, 1);  
    	this.mode = 0;  
		this.update();
	}
	
	this.setmode = function (num)
	{
    	this.mode = num; 
		this.create ();
		this.Container.innerHTML = this.HTML;  
	}
	
	this.fwd = function ()
	{
    	this.oDate = new Date(this.Year, this.Month + 1, 1);  
		this.update();
	}
	
	this.getvalue = function ()
	{
	    return new Date(this.Year, this.Month, this.Day);  
	}
	
	this.back = function ()
	{
    	this.oDate = new Date(this.Year, this.Month - 1, 1);  
		this.update();
	}
	
	this.update = function ()
	{
		this.Month = this.oDate.getMonth();
		this.Day = this.oDate.getDate();
		this.Year = this.oDate.getFullYear(); 
		this.NextMonth = new Date(this.Year, this.Month + 1, 1); 
		this.WeekStart = this.oDate.getDay(); 
		// Get the number of days in current month 
		this.MonthDays = Math.round((this.NextMonth.getTime() - this.oDate.getTime()) / 86400000) + 1; 
		this.create ();
		this.Container.innerHTML = this.HTML; 
	}
	this.mode = 'date';
	this.create = function ()
	{
	    this.HTML='';
	    if (!this.Data)
	    {
		    this.HTML += '<table cellpadding=0 cellspacing="0"'; 
		    this.HTML += ' style="border:solid 1px;border-color:#eeeeee #c9d7f1 #999999 #eeeeee;width:'+this.TableSize+'px;height:'+this.TableSize+'px" bgcolor="#c9d7f1"'; 
		    this.HTML += ' align="' + (this.Data ? 'left' : 'right') + '" '; 
		    this.HTML += ' width="'+this.TableSize+'" height="'+this.TableSize+'">'; 
		    this.HTML += '<tr><td colspan=3><img src="/webservices/images/clear.gif" height=10></td></tr>'; 
		    this.HTML += '<tr><td><img src="/webservices/images/clear.gif" height=10></td><td>'; 
		}
		
		this.HTML += '<table ' + (this.Data?'class="CalendarTable"':'') + ' cellspacing="0" width="'+this.TableSize+'" height="'+this.TableSize+'" align=center>'; 
		
		if (this.mode==1)
		{
			// Title bar 
			this.HTML += '<tr bgcolor="#c9d7f1">';  
			this.HTML += '<td colspan="7" align=center class="Title">Choose a month</td>';  
			this.HTML += '</tr><tr>'; 
			
			for (var x=0;x<Calendar.MonthNames.length;x++)
			{
				this.HTML += '<td width="33%" align=center class="WeekName">';
				this.HTML += '<a href="javascript:void(0)" ';
				this.HTML += ' onclick="window.datepicker[' + this.id + '].setmonth ('+x+')"';
				this.HTML += ' style="font-size:7pt;font-weight:'+(x==this.Month?'bold;color:red':'')+'">' + Calendar.MonthNames[x] + '</a>';
				this.HTML += '</td>';
				if ((x+1)%3==0) this.HTML += '</tr><tr>';
			}
			this.HTML += '</tr>';
			
		}
		else if (this.mode==2)
		{
			// Title bar 
			this.HTML += '<tr bgcolor="#c9d7f1">';  
			this.HTML += '<td colspan="7" align=center class="Title">Choose a year</td>';  
			this.HTML += '</tr><tr>'; 
			
			for (var x=this.Year - 10, i=0;x<this.Year + 10;x++)
			{
				this.HTML += '<td width="33%" align=center class="WeekName">';
				this.HTML += '<a href="javascript:void(0)" ';
				this.HTML += ' onclick="window.datepicker[' + this.id + '].setyear ('+x+')"';
				this.HTML += ' style="font-size:7pt;font-weight:'+(x==this.Year?'bold;color:red':'')+'">' + x + '</a>';
				this.HTML += '</td>';
				if ((i+1)%4==0) this.HTML += '</tr><tr>';
				i++;
			}
			this.HTML += '</tr>';
			
		}
		else 
		{
			// Title bar 
			var stamp = new Date ().getTime();
			var style = this.style?('style="'+this.style.dayname+'"'):'';
			this.HTML += '<tr bgcolor="#c9d7f1">'; 
			this.HTML += '<td align=center class="Title"><a' + style + ' href="javascript:void(0)" '; 
			this.HTML += 'onclick="'; 
			this.HTML += 'window.datepicker[' + this.id + '].back ()'; 
			this.HTML += '">&#171;</a></td>'; 
			this.HTML += '<td colspan="5" align=center class="Title" ' + style + '>'; 
			this.HTML += '<a href="javascript:void(0)" onclick="window.datepicker[' + this.id + '].setmode (1)">' + Calendar.MonthNames[this.Month] + '</a>'; 
			this.HTML += ' ';
			this.HTML += '<a href="javascript:void(0)" onclick="window.datepicker[' + this.id + '].setmode (2)">' + this.Year + '</a>'; 
			this.HTML += '</td>'; 
			this.HTML += '<td align=center class="Title"><a' + style + ' href="javascript:void(0)" '; 
			this.HTML += 'onclick="'; 
			this.HTML += 'window.datepicker[' + this.id + '].fwd ()'; 
			this.HTML += '">&#187;</a></td>'; 
			this.HTML += '</tr>'; 
			// Week Names  
			var wm=new Array ('S','M','T','W','T','F','S');
			this.HTML += '<tr bgcolor="#c9d7f1" class="WeekName">'; 
			for (var m=0;m<7;m++) 
			    this.HTML += '<td ' + style + ' width="'+this.PanelSize+'" '+(this.Data?'height="24"':'')+' class="WeekName" id="wm'+m+'">'+(this.Data?Calendar.DayNames[m]:wm[m])+'</td>'; 

			this.HTML += '</tr>'; 
			this.HTML += '<tr>'; 
			// Fill the previous month days with space 
			for(var DayCounter = 0; DayCounter < this.WeekStart; DayCounter++) 
			{ 
				this.HTML += '<td>&nbsp;</td>'; 
			} 
			// Populate current month 
			for(var DayCounter = 1; DayCounter < this.MonthDays; DayCounter++) 
			{ 
				var wd=(DayCounter + this.WeekStart) % 7;
				if(wd == 1) this.HTML += '<tr>'; 
				
				var data = '';
				if (this.Data)
				{
				    for (var x=0;x<this.Data[DayCounter].length;x++)
				    {
						data += this.mask (this.Data[DayCounter][x]); 
				    }
				}
				
				this.css = wd<2?'WeekEnd':'WeekDay';
				this.css = DayCounter == this.Day ? 'SelectedDay' : this.css;
				this.hue = DayCounter == this.Day ? 'white' : 'blue';
				this.HTML += '<td width="' + this.PanelSize + '" height="' + this.PanelSize + '" ';
				this.HTML += this.Data ? ' style="' + style + 'text-align:left" valign="top" ' : '';
				if (!this.linktype) 
					this.HTML += ' onclick="window.datepicker[' + this.id + '].setdate (' + DayCounter + ')" '; 
				this.HTML += 'class="' + this.css + '">'; 
				if (this.linktype) 
				    this.HTML += '<a style="color:' + this.hue + '" href="./#report/search/action/event/stamp/' + stamp + '/op/xms/s1/' + this.Year + '-' + (this.Month+1) + '-'+DayCounter+'">' + DayCounter + data + '</a>'; 
				else this.HTML += DayCounter + data; 
				this.HTML += '</td>';  
				
				if((DayCounter + this.WeekStart) % 7 == 0) this.HTML += '</tr>'; 
			} 
			// Fill the next month days with space 
			for(var j = (42 - (this.MonthDays + this.WeekStart)), DayCounter = 0; DayCounter <= j; DayCounter++) 
			{ 
				this.HTML += '<td>&nbsp;</td>'; 
				if((j - DayCounter) % 7 == 0)  this.HTML += '</tr>'; 
			} 
		}
		
		
		this.HTML += '</table>'; 
		
	    if (!this.Data)
	    {
		    this.HTML += '</td><td><img src="/webservices/images/clear.gif" height=10></td>'; 
		    this.HTML += '<tr><td colspan=3><img src="/webservices/images/clear.gif" height=10></td></tr>';  
		    this.HTML += '</table>'; 
        }
	}
	
	this.create ();
	this.onupdate = function () {}
	this.draw = function (ContainerId)
	{
		// Check whether the Container Id is null 
		if(ContainerId != null) 
		{ 
			this.Container = document.getElementById(ContainerId); 
			// If an object exists with the given ContainerId insert the Calendar into the object 
			if(this.Container) 
				document.getElementById(ContainerId).innerHTML = this.HTML; 
			// Else create an element with the given id and insert the calendar 
			else 
				document.write('<div id="' + ContainerId + '">' + this.HTML + '</div>'); 
		} 
		else 
		{ 
		    return this.HTML;
		    // -----------------------------------------------------------CLIP HERE
			// Loop until a unique id is obtained for the container 
			do 
			{ 
				ContainerId = 'tblCalendar' + Math.round(Math.random() * 1000); 
			} 
			while(document.getElementById(ContainerId)); 
			// create an element with the new id and insert the calendar 
			document.write('<div id="' + ContainerId + '">' + this.HTML + '</div>'); 
		} 
		// Returns Id of the element containing the calendar 
		return ContainerId; 
	}
} 

Calendar.MonthNames = new Array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'); 
Calendar.DayNames = new Array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'); 
 
	
Calendar.NOREPEAT	= -1;
Calendar.DAILY		= 0;
Calendar.WEEKLY		= 1;
Calendar.MONTHBYDATE= 2;
Calendar.MONTHBYWEEK= 3;
Calendar.YEARLY		= 4;
Calendar.WEEKDAY	= 5;
Calendar.CUSTOM		= 7;


Calendar.scope = function (s1,s2,tp,ui)
{
	var d1=new Date (dateUS(s1)); if (isNaN(d1)) alert ('Invalid date ' + s1);
	var wk= vb_get_week(d1);
	var da= Calendar.DayNames[d1.getDay()];
	switch (parseInt(tp))
	{
		case Calendar.DAILY:
			return 'Every '+fancynum (ui)+' day until ' + dateInput (s2);
			break;
		case Calendar.WEEKLY:
			return 'Every '+fancynum (ui)+' week on '+da+' until ' + dateInput (s2);
			break;
		case Calendar.MONTHBYDATE:
			return 'Every '+fancynum (ui)+' month on the '+ordnum (d1.getDate())+' day of the month until ' + dateInput (s2);
			break;
		case Calendar.MONTHBYWEEK:
			return 'Every '+fancynum (ui)+' month on the '+ordnum (wk)+' '+da+' until ' + dateInput (s2);
			break;
		case Calendar.YEARLY:
			return 'Every '+fancynum (ui)+' year until ' + dateInput (s2);
			break;
		case Calendar.WEEKDAY:
			return 'Every '+fancynum (ui)+' weekday (M-F) until ' + dateInput (s2);
			break;
		case Calendar.CUSTOM:
			return 'On various dates, until ' + dateInput (s2);
			break;
	}
	return 'Does not repeat';
}

function vb_get_week(date) { 
	var d1=new Date (date);
    var today = d1.getDate(); 
    return (Math.floor(today / 7) - (today % 7 == 0 ? 1 : 0)) + 1 ; 
} 

function fancynum (i)
{
	var x=i%10;
	var s=i+'th';
	switch (x)
	{
		case 1:
			s='';
			break; 
		case 2:
			s='other';
			break; 
		case 3:
			s=i+'rd';
			break; 
	}
	return s;
}
function ordnum (i)
{
	var x=i%10;
	var s=i+'th';
	switch (x)
	{
		case 1:
			s=i+'st';
			break; 
		case 2:
			s=i+'nd';
			break; 
		case 3:
			s=i+'rd';
			break; 
	}
	return s;
}